package com.example.tagsdemo.builder;

import com.example.tagsdemo.dto.ColumnDTO;
import com.google.common.collect.Sets;
import org.apache.commons.lang3.StringUtils;

import java.util.List;
import java.util.Set;

public class ConcreteBuilder extends DDLBuilder {

    private String tableName;
    private String remarks;
    private List<ColumnDTO> columnDTOList;

    private static final String NOT_NULL = " NOT NULL ";
    private static final String DEFAULT_NULL = " DEFAULT NULL ";
    private static final String DEFAULT = " DEFAULT ";
    private static final String PRIMARY_KEY = " PRIMARY KEY ";
    private static final String USING_BTREE = " USING BTREE ";

    private static final Set<String> NOT_APPEND_LENGTH = Sets.newHashSet();
    static {
        NOT_APPEND_LENGTH.add("DATE");
        NOT_APPEND_LENGTH.add("DATETIME");
        NOT_APPEND_LENGTH.add("TIMESTAMP");
    }
    private StringBuffer sb = new StringBuffer();

    public ConcreteBuilder(String tableName,String remarks,List<ColumnDTO> columnDTOList) {
        this.tableName = tableName;
        this.remarks = remarks;
        this.columnDTOList = columnDTOList;
    }

    private void bulidTableName() {
        sb.append(" CREATE TABLE `")
            .append(tableName).append("` ( ");
    }

    private void bulidColumns() {
        StringBuffer primaryKey = new StringBuffer();
        columnDTOList.forEach(e-> {
            sb.append(" `").append(e.getColumn()).append("` ")
                .append(e.getType());
            if (!NOT_APPEND_LENGTH.contains(e.getType())) {
                sb.append("(").append( e.getLength()).append(") ");
            }

            if (StringUtils.equals("1",e.getIsNull().toString())) {
                sb.append(NOT_NULL);
            }
            String value = e.getDefaultValue();
            if (!StringUtils.equals("",e.getDefaultValue())) {
                if (StringUtils.equalsIgnoreCase(e.getType(),"DATE") ||
                        StringUtils.equalsIgnoreCase(e.getType(),"DATETI,E")) {
                    sb.append(DEFAULT_NULL);
                }else if(StringUtils.equalsIgnoreCase(e.getType(),"TIMESTAMP")){
                   sb.append(" NULL DEFAULT ").append(e.getDefaultValue());
                }else{
                    sb.append(DEFAULT).append(e.getDefaultValue());
                }
            }
            if (StringUtils.isNotBlank(e.getColumnRemarks())) {
                sb.append(" COMMENT ").append("'").append(e.getColumnRemarks()).append("'");
            }
            sb.append(",");

            if (StringUtils.equals("1",e.getIsPrimaryKey().toString())) {
                primaryKey.append(("`")).append(e.getColumn()).append(("`")).append(",");
            }

        });
        String primary = primaryKey.substring(0, primaryKey.length() - 1);
        sb.append(PRIMARY_KEY).append("(").append(primary).append(")").append(USING_BTREE)
                .append(" )");

    }

    private void bulidCore() {
        sb.append(" ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_520_ci ");
    }

    private void bulidTableRemarks() {
        sb.append(" COMMENT='")
                .append(remarks).append("'; ");
    }

    @Override
   public String build() {
        this.bulidTableName();
        this.bulidColumns();
        this.bulidCore();
        this.bulidTableRemarks();
        return sb.toString();
    }
}
